﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.WebUserControls.Frame
{
    /// <summary>
    /// DirectorateListFrame class
    /// </summary>
    public partial class DirectorateListFrame : UserControl
    {
        #region Properties

        /// <summary>
        /// Gets or sets the hidden field organization id.
        /// </summary>
        /// <value>
        /// The hidden field organization id.
        /// </value>
        [Category("Manipulation")]
        public string HiddenFieldOrganizationId
        {
            get { return (string)ViewState["HiddenFieldOrganizationId"] ?? string.Empty; }
            set { ViewState["HiddenFieldOrganizationId"] = value; }
        }

        /// <summary>
        /// Gets or sets the organization id.
        /// </summary>
        /// <value>
        /// The organization id.
        /// </value>
        [Category("Manipulation")]
        public int OrganizationId
        {
            get { return ViewState["OrganizationId"] != null ? (int)ViewState["OrganizationId"] : -1; }
            set { ViewState["OrganizationId"] = value; }
        }

        #endregion Properties

        #region Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack)
            //{
            DirectorateFilter filter = ViewState["filter"] as DirectorateFilter ??
                                       new DirectorateFilter { OrganizationId = -1 };
            var organisationIdControl = Parent.FindControl(HiddenFieldOrganizationId) as HiddenField;
            int organisationId;
            if (organisationIdControl != null && int.TryParse(organisationIdControl.Value, out organisationId))
            {
                filter.OrganizationId = organisationId;
            }
            else
            {
                filter.OrganizationId = OrganizationId;
            }

            if (!Page.IsPostBack)
            {
                ViewState["filter"] = filter;
                ViewState["sortingButton"] = ViewState["sortingButton"] ?? LinkButtonAll.ClientID;
            }
            //}
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as DirectorateFilter;
            if (filter != null)
            {
                filter.IncludeInactive = CheckBoxInactive.Checked;
                GridViewDirectorate.DataBind();
            }
        }

        /// <summary>
        /// Handles the Command event of the FilterByLetters control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            //Thread.Sleep(3000);
            var filter = ViewState["filter"] as DirectorateFilter;
            if (filter != null)
            {
                Collection<string> beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);
                GridViewDirectorate.PageIndex = 0;
                GridViewDirectorate.DataBind();

                var letterGroupId = (string)ViewState["sortingButton"];
                var letterGroupButton = FindControl(letterGroupId) as LinkButton;
                if (letterGroupButton != null)
                {
                    var clickedButton = sender as LinkButton;
                    if (clickedButton == null)
                    {
                        return;
                    }

                    letterGroupButton.CssClass = string.Empty;
                    clickedButton.CssClass = "hg-blue";
                    ViewState["sortingButton"] = clickedButton.ClientID;
                }
            }
        }

        /// <summary>
        /// Handles the Selecting event of the ObjectDataSourceDirectorate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSourceDirectorate_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as DirectorateFilter;
            if (filter != null)
            {
                e.InputParameters["filter"] = filter;
            }
        }

        /// <summary>
        /// Handles the Click event of the LinkButtonCreate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkButtonCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                            "~/Directorate/DirectorateDetails.aspx?organisationId={0}", OrganizationId));
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonDirectorateName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonDirectorateName_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                int directorateId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                DirectorateBO.SetDirectorateStatus(directorateId, true);
            }

            Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                            "~/Directorate/DirectorateDetails.aspx?directorateId={0}", e.CommandName));
        }

        #endregion Events
    }
}